package com.crocodileQ.controller;

import com.crocodileQ.common.AppException;
import com.crocodileQ.common.PageResult;
import com.crocodileQ.common.R;
import com.crocodileQ.domain.VO.ArticleDetailVO;
import com.crocodileQ.domain.VO.ArticleListVO;
import com.crocodileQ.domain.VO.HotArticleVO;
import com.crocodileQ.enums.AppExceptionMsgEnum;
import com.crocodileQ.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author 鳄鱼杆
 */
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    /**
     * 查询热门文章
     * @return
     */
    @GetMapping("/hotArticleList")
    public R<List<HotArticleVO>> hotArticleList(){

        return articleService.hotArticleList();
    }

    /**
     * 分页查询文章
     * @return
     */
    @GetMapping("/articleList")
    public R<PageResult<ArticleListVO>> articleList(Integer pageNum, Integer pageSize, Long categoryId,String name){
        //判断参数
        if(pageNum <= 0 || pageSize <= 0){
            throw new AppException(AppExceptionMsgEnum.WRONG_PAGE_PARAM);
        }

        return articleService.articleList(pageNum,pageSize,categoryId,name);
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<ArticleDetailVO> getArticleDetail(@PathVariable("id") Long id){

        return articleService.getArticleDetail(id);
    }

}
